<?php
namespace Api\Service;

/**
 * 地址
 */
class AddressService
{
    /**
     * 用户收货地址
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function lists($user_id)
    {
        $adds = M('address')
            ->field('id,mobile,name,province,city,region,address,is_default')
            ->where([
                'user_id'   => $user_id,
                'is_delete' => 0,
            ])
            ->order('is_default desc, id desc')
            ->select();

        return $adds ? $adds : [];
    }

    /**
     * 添加收获地址
     * @param [type] $user_id [description]
     * @param [type] $add     [description]
     */
    public function add($user_id, $add, $is_default = false)
    {
        $add['user_id']    = $user_id;
        $add['is_default'] = 0;
        $add['add_time']   = CUR_DATETIME;
        $res               = M('address')->add($add);

        if ($is_default) {
            $this->add_after($user_id, $res);
        }

        return $res;
    }

    /**
     * 删除收货地址
     * @param  [type] $user_id [description]
     * @param  [type] $add_id  [description]
     * @return [type]          [description]
     */
    public function delete($user_id, $add_id)
    {
        $data = [
            'is_delete'   => 1,
            'delete_time' => CUR_DATETIME,
        ];
        $res = M('address')->where([
            'id'      => $add_id,
            'user_id' => $user_id,
        ])->save($data);

        return $res;
    }

    /**
     * 添加完收货地址后的操作
     * @param [type] $user_id    [description]
     * @param [type] $address_id [description]
     */
    protected function add_after($user_id, $address_id)
    {
        // 设置默认收货地址
        $this->setDefault($user_id, $address_id);
    }

    /**
     * 设置默认收货地址
     * @param [type] $add_id [description]
     */
    public function setDefault($user_id, $add_id)
    {
        // 取消默认
        $res1 = M('address')->where([
            'user_id'    => $user_id,
            'is_default' => 1,
        ])->save(['is_default' => 0]);

        // 设置默认
        $res2 = M('address')->where([
            'id'      => $add_id,
            'user_id' => $user_id,
        ])->save(['is_default' => 1]);

        return $res1 && $res2;
    }

    /**
     * 获取用户默认收获地址
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function getDefault($user_id)
    {
        $add = M('address')
            ->field('id,mobile,name,province,city,region,address')
            ->where([
                'user_id'    => $user_id,
                'is_delete'  => 0,
                'is_default' => 1,
            ])->find();
        return $add ? $add : [];
    }

    /**
     * 更新收货地址
     * @param  [type] $user_id [description]
     * @param  [type] $add_id  [description]
     * @param  [type] $data    [description]
     * @return [type]          [description]
     */
    public function update($user_id, $add_id, $data)
    {
        $res = M('address')->where([
            'id'      => $add_id,
            'user_id' => $user_id,
        ])->save($data);

        return true;
    }
}
